*This file creates a combined Finnish and Norwegian local dataset for triple-difference estimations.
*See Online Appendix Section C.D for further details on Norwegian data collection and construction.

*Import occupational structure data for Norway

use "$path/norway_occupation_muni.dta", clear

*Import the shock data for Norway

preserve
use "$path/fylke_bartik_exposure.dta", clear
replace Fylke=trim(Fylke)
tempfile fylke2
save `fylke2'
restore

joinby  Fylke using `fylke2', unmatched(both)

drop if municipality==""

drop _merge

egen muni=group(municipality  municipalityno)
gen post=(year>1940)
gen bartik_post=exposure_std*post

gen bartik_h_post=post*exposure_std_h
gen bartik_l_post=post*exposure_std_l
gen bartik_ntp_st_post=post*exposure_std_ntp

*Generate 1930 controls
gen agrishare_1930=agrishare_ if year==1930
bys municipality  municipalityno: egen agrishare_1930_2=mean(agrishare_1930)   
gen industryshare_1930=industryshare_ if year==1930
bys municipality  municipalityno: egen industryshare_1930_2=mean(industryshare_1930)   
gen total_1930=total_ if year==1930
bys municipality  municipalityno: egen total_1930_2=mean(total_)   
drop agrishare_1930 industryshare_1930 total_1930

gen ln_total_1930=ln(total_1930_2)

gen ln_total=ln(total_)

est clear

keep industryshare_ agrishare_ ln_total muni year  bartik_post post city  bartik_h_post bartik_l_post bartik_ntp_st_post 


*Import and append with Finland data

preserve
use "$path/elinkeino_forregs.dta", clear
rename manushare_a industryshare_
rename agrishare_a agrishare_
rename ln_pop ln_total
rename code muni
keep industryshare_ agrishare_ ln_total muni year bartik_post post  city  bartik_h_post bartik_l_post bartik_ntp_st_post
gen finland=1
tempfile fin
save `fin'
restore

append using `fin'

replace finland=0 if finland==.

drop if year==1940|year==1970 // not in Norway

*Generate county-muncipality units.
egen muni_unique=group(muni finland)
egen country_year=group(year finland)

gen agrishare_1930=agrishare_ if year==1930
bys muni_unique  : egen agrishare_1930_2=mean(agrishare_1930)   
gen industryshare_1930=industryshare_ if year==1930
bys muni_unique  : egen industryshare_1930_2=mean(industryshare_1930)   
gen ln_total_1930=ln_total if year==1930
bys muni_unique  : egen ln_total_1930_2=mean(ln_total_1930)   
drop agrishare_1930 industryshare_1930 ln_total_1930

gen pop_1930=round(exp(ln_total_1930_2))

global controls  c.industryshare_1930_2#year c.agrishare_1930_2#year c.ln_total_1930_2#year city#year 

*Generate exposure variables
gen  bartik_post_finland=bartik_post*finland
gen finland_post=post*finland
gen  bartik_h_post_finland=bartik_h_post*finland
gen  bartik_l_post_finland=bartik_l_post*finland

*Label variables
label variable year "Year"
label variable agrishare_ "Agriculture share"
label variable industryshare_ "Industry share"
label variable city "City"
label variable muni "Municipality identifier"
label variable post "Post-treatment indicator"
label variable ln_total "Log of total population"
label variable finland "Finland indicator"
label variable muni_unique "Unique municipality identifier"
label variable country_year "Country-year identifier"
label variable agrishare_1930_2 "Agriculture share in 1930"
label variable industryshare_1930_2 "Industry share in 1930"
label variable ln_total_1930_2 "Log of total in 1930"
label variable pop_1930 "Population in 1930"
label var bartik_post_finland "Exposure x Post x Finland"
label var bartik_post "Exposure x Post"
label variable finland_post "Finland x Post"
label var bartik_h_post_finland "HS Exposure x Post x Finland"
label var bartik_h_post "HS Exposure x Post"
label var bartik_l_post_finland "LS Exposure x Post x Finland"
label var bartik_l_post "LS Exposure x Post"

est clear

*Estimations

foreach x in industryshare_   agrishare_  {
reghdfe `x' bartik_post_finland bartik_post finland_post [aw=pop_1930], a(country_year muni_unique  $controls) cl(muni_unique)
estadd local controls "\checkmark"
estadd local ife "\checkmark"
estadd local yfe "\checkmark"
estadd local mun "1100" 
estadd local finmun "356" 
estadd local established "Yes" 

sum `x'
estadd scalar ymean=r(mean)
estimates store a`x' 
}

***High skill and loq skill separately

foreach x in industryshare_   agrishare_ {
reghdfe `x' bartik_h_post_finland bartik_l_post_finland  bartik_h_post bartik_l_post finland_post [aw=pop_1930], a(country_year muni_unique  $controls) cl(muni_unique)
estadd local controls "\checkmark"
estadd local ife "\checkmark"
estadd local yfe "\checkmark"
estadd local mun "1100" 
estadd local finmun "356" 
estadd local established "Yes" 

sum `x'
estadd scalar ymean=r(mean)
estimates store ahs`x' 
}

**Without Established

preserve
gen  bartik_ntp_st_post_finland=bartik_ntp_st_post*finland

replace bartik_post_finland=bartik_ntp_st_post_finland
replace bartik_post=bartik_ntp_st_post

foreach x in industryshare_   agrishare_ {
reghdfe `x' bartik_post_finland bartik_post finland_post [aw=pop_1930], a(country_year muni_unique  $controls) cl(muni_unique)
estadd local controls "\checkmark"
estadd local ife "\checkmark"
estadd local yfe "\checkmark"
estadd local mun "1100" 
estadd local finmun "356" 
estadd local established "No" 

sum `x'
estadd scalar ymean=r(mean)
estimates store antp`x' 
}
restore 

*Label variables
label var bartik_h_post_finland "HS Exposure x Post x Finland"
label var bartik_h_post "HS Exposure x Post"
label var bartik_l_post_finland "LS Exposure x Post x Finland"
label var bartik_l_post "LS Exposure x Post"

esttab aindustryshare_  antpindustryshare_ ahsindustryshare_ aagrishare_ antpagrishare_ ahsagrishare_ ///
using   "$tables/DDD_estimates_municipality.tex",  ///
mgroups("Manufacturing Employment Share" "Agriculture Employment Share" ,   pattern(1 0 0 1 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}) ) ///
replace  keep(bartik_post_finland bartik_post bartik_h_post_finland bartik_l_post_finland  bartik_h_post bartik_l_post) order(bartik_post_finland bartik_post) nonotes ///
b(%9.3f) se(%9.3f) stats(N  mun established ife yfe  controls, fmt(0 3) layout(@ @ @) ///
labels("N" "Municipalities" "Established"  "Municipality FE" "Year-country FE"  "Municipality controls x Year" ///
)) ///
label star(* .1 ** .05 *** .01) nodepvars nomtitle 
